package com.banmao.mall.controller.admin;

import com.banmao.mall.model.bo.admin.config.ExpressBO;
import com.banmao.mall.model.bo.admin.config.MallBO;
import com.banmao.mall.model.bo.admin.config.OrderBO;
import com.banmao.mall.model.bo.admin.config.WxBO;
import com.banmao.mall.model.vo.BaseRespVO;
import com.banmao.mall.service.admin.ConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
public class ConfigController {

    @Autowired
    ConfigService configService;
    @RequiresPermissions("admin:config:mall:list")
    @RequestMapping(value = "/config/mall", method = RequestMethod.GET)
    public BaseRespVO mallGet() {
        //Request URL: http://182.92.235.201:8083/admin/config/mall
        //Request Method: GET
//        "cskaoyanmall_mall_name": "litemall",
//        "cskaoyanmall_mall_qq": "738696120",
//        "cskaoyanmall_mall_phone": "021-xxxx-xxxx",
//        "cskaoyanmall_mall_address": "上海"
        MallBO mallBO = configService.mallSelect();
        return BaseRespVO.ok(mallBO);
    }
    @RequiresPermissions("admin:config:mall:updateConfigs")
    @RequestMapping(value = "/config/mall",method = RequestMethod.POST)
    public BaseRespVO mallPost(@Validated @RequestBody MallBO mallBO, BindingResult result) {
        //Request URL: http://182.92.235.201:8083/admin/config/mall
        //Request Method: POST
        if (result.hasFieldErrors()) {
            //BO仅限制了参数不能为空
            return BaseRespVO.error("参数不能为空",400);
        }
        int status = configService.mallUpdate(mallBO);
        if (status == 500) {
            return BaseRespVO.error("数据库繁忙",500);
        }
        return BaseRespVO.ok(null);
    }

    @RequiresPermissions("admin:config:express:list")
    @RequestMapping(value = "/config/express",method = RequestMethod.GET)
    public BaseRespVO expressGet() {
//        Request URL: http://localhost:8083/admin/config/express
//        Request Method: GET
//        响应体：
//        data: {cskaoyanmall_express_freight_value: "8", cskaoyanmall_express_freight_min: "88"}
//        errmsg: "成功"
//        errno: 0
        ExpressBO expressBO = configService.expressSelect();
        return BaseRespVO.ok(expressBO);
    }
    @RequiresPermissions("admin:config:express:updateConfigs")
    @RequestMapping(value = "/config/express", method = RequestMethod.POST)
    public BaseRespVO expressPost(@Validated @RequestBody ExpressBO expressBO, BindingResult result) {
        //Request URL: http://182.92.235.201:8083/admin/config/express
        //Request Method: POST
        //请求体：
        //{
        //    "cskaoyanmall_express_freight_value": "8",
        //        "cskaoyanmall_express_freight_min": "88"
        //}
        //响应体：{"errno":0,"errmsg":"成功"}
        if (result.hasFieldErrors()) {
            //BO仅限制了参数不能为空
            return BaseRespVO.error("参数不能为空",400);
        }
        int status = configService.expressUpdate(expressBO);
        if (status == 500) {
            return BaseRespVO.error("数据库繁忙",500);
        }
        return BaseRespVO.ok(null);
    }
    @RequiresPermissions("admin:config:order:list")
    @RequestMapping(value = "/config/order", method = RequestMethod.GET)
    public BaseRespVO orderGet() {
        //Request URL: http://182.92.235.201:8083/admin/config/order
        //Request Method: GET
        //响应体：
        //{
        //    "errno": 0,
        //    "data": {
        //        "cskaoyanmall_order_unconfirm": "7",
        //        "cskaoyanmall_order_unpaid": "30",
        //        "cskaoyanmall_order_comment": "7"
        //    },
        //    "errmsg": "成功"
        //}

        OrderBO orderBO = configService.orderSelect();
        return BaseRespVO.ok(orderBO);
    }
    @RequiresPermissions("admin:config:order:updateConfigs")
    @RequestMapping(value = "/config/order", method = RequestMethod.POST)
    public BaseRespVO orderPost(@Validated @RequestBody OrderBO orderBO, BindingResult result) {
        //Request URL: http://182.92.235.201:8083/admin/config/order
        //Request Method: POST
        //请求体：
        //{
        //        "cskaoyanmall_order_unconfirm": "7",
        //        "cskaoyanmall_order_unpaid": "30",
        //        "cskaoyanmall_order_comment": "7"
        //}
        //响应体：{"errno":0,"errmsg":"成功"}
        if (result.hasFieldErrors()) {
            //BO仅限制了参数不能为空
            return BaseRespVO.error("参数不能为空",400);
        }
        int status = configService.orderUpdate(orderBO);
        if (status == 500) {
            return BaseRespVO.error("数据库繁忙",500);
        }
        return BaseRespVO.ok(null);
    }
    @RequiresPermissions("admin:config:wx:list")
    @RequestMapping(value = "/config/wx", method = RequestMethod.GET)
    public BaseRespVO wxGet() {
        //Request URL: http://182.92.235.201:8083/admin/config/wx
        //Request Method: GET
        //请求体：
        //{
        //    "errno": 0,
        //    "data": {
        //        "cskaoyanmall_wx_share": "true",
        //        "cskaoyanmall_wx_index_brand": "4",
        //        "cskaoyanmall_wx_index_topic": "4",
        //        "cskaoyanmall_wx_index_hot": "7",
        //        "cskaoyanmall_wx_catlog_goods": "4",
        //        "cskaoyanmall_wx_catlog_list": "4",
        //        "cskaoyanmall_wx_index_new": "6"
        //    },
        //    "errmsg": "成功"
        //}
        WxBO wxBO = configService.wxSelect();
        return BaseRespVO.ok(wxBO);
    }
    @RequiresPermissions("admin:config:wx:updateConfigs")
    @RequestMapping(value = "/config/wx", method = RequestMethod.POST)
    public BaseRespVO wxPost(@Validated @RequestBody WxBO wxBO, BindingResult result) {
        if (result.hasFieldErrors()) {
            //BO仅限制了参数不能为空
            return BaseRespVO.error("参数不能为空",400);
        }
        int status = configService.wxUpdate(wxBO);
        if (status == 500) {
            return BaseRespVO.error("数据库繁忙",500);
        }
        return BaseRespVO.ok(null);
    }


}
